package cn.ekuma.epos.datalogic.local.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.openbravo.bean.Product;
import cn.ekuma.epos.datalogic.local.AbstractBaseDAO;
import cn.ekuma.epos.datalogic.util.DataLogicUtil;
import cn.ekuma.epos.db.table.I_Product;
import cn.ekuma.epos.db.table.Table;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ProductDAO extends AbstractBaseDAO<Product>{
	 

	public ProductDAO(SQLiteOpenHelper db) {
		super(db);
	}

	public static void onCreate(SQLiteDatabase sqLiteDatabase) {
	    sqLiteDatabase.execSQL(
	            "CREATE TABLE PRODUCTS ("+
    "ID VARCHAR(1024) PRIMARY KEY,"+
    "REFERENCE VARCHAR(1024) NOT NULL,"+
    "CODE VARCHAR(1024) NOT NULL,"+
    "CODETYPE VARCHAR(1024),"+
    "NAME VARCHAR(1024) NOT NULL,"+
    "PRICEBUY DOUBLE PRECISION NOT NULL,"+
    "PRICESELL DOUBLE PRECISION NOT NULL,"+
    "CUSTOMERPRICESELL DOUBLE PRECISION NOT NULL DEFAULT 0.0,"+
    "CATEGORY VARCHAR(256) NOT NULL,"+
    "TAXCAT VARCHAR(256) NOT NULL,"+
    "ATTRIBUTESET_ID VARCHAR(256),"+
    "STOCKCOST DOUBLE PRECISION,"+
    "STOCKVOLUME DOUBLE PRECISION,"+
    "ISCOM SMALLINT DEFAULT 0 NOT NULL,"+
    "ISSCALE SMALLINT DEFAULT 0 NOT NULL,"+
    "ATTRIBUTES BLOB,"+
    "ORIGIN VARCHAR(256),"+
    "QUANTITY DOUBLE PRECISION NOT NULL DEFAULT 1.0,"+
    "UNIT VARCHAR(256),"+
    "LASTMODIFIED TIMESTAMP,"+
    "DURABILITY INTEGER NOT NULL DEFAULT 0,"+
    "SIMILARCODE VARCHAR(256),"+
    "MANUFACTUR VARCHAR(256),"+
    "MNEMONIC VARCHAR(256),"+
    "ISCATALOG SMALLINT DEFAULT 0 NOT NULL,"+
    "CATORDER INTEGER,"+
    "MANUFACTURING SMALLINT DEFAULT 0 NOT NULL,"+
    "WHOLESALEPRICE DOUBLE PRECISION NOT NULL DEFAULT 0.0,"+
    "CURDATE TIMESTAMP,"+
    "WEIGHT DOUBLE PRECISION NOT NULL DEFAULT 0.0,"+
    "ISBOMSTORE SMALLINT DEFAULT 1 NOT NULL,"+
    "SPECIFICATION VARCHAR(256)"+
    ");");
	}

	public String getKeyColumn() {
		return ID;
	}

	@Override
	public Product readValues(Cursor dr, Product product) {
		if(product==null)
			product=new Product();
		product.setID(dr.getString(0));
		product.setReference(dr.getString(1));
		product.setCode(dr.getString(2));
		product.setName(dr.getString(3));
		product.setCom(dr.getLong(4)==1);
		
		product.setScale(dr.getLong(5)==1);
		product.setPriceBuy(dr.getDouble(6));
		product.setPriceSell(dr.getDouble(7));
		product.setTaxCategoryID(dr.getString(8));
		product.setCategoryID(dr.getString(9));
		
		product.setAttributeSetID(dr.getString(10));
		product.setAttributes((HashMap) DataLogicUtil.readSerializable(dr.getBlob(11)));
		product.setM_dCustomerPrice(dr.getDouble(12));
		product.setM_origin(dr.getString(13));
		product.setM_quantity(dr.getDouble(14));
		
		product.setM_unit(dr.getString(15));
		product.setM_sManufacturer(dr.getString(16));
		product.setM_sSimilarCode(dr.getString(17));
		product.setM_iDurability(dr.getInt(18));
		product.setMnemonic(dr.getString(19));
		
		product.setLastModified(new Date(dr.getLong(20)));
		product.setM_bManufacturing(dr.getLong(21)==1);
		product.setM_dWholesalePrice(dr.getDouble(22));
		product.setCurDate(new Date(dr.getLong(23)));
		product.setWeight(dr.getDouble(24));
		
		product.setBomStore(dr.getLong(25)==1);
		product.setSpecification(dr.getString(26));
		return product;
	}

	@Override
	public String[] getTableColumns() {
		return new String[]{
				I_Product.ID,
				I_Product.REFERENCE, 
				I_Product.CODE, 
				I_Product.NAME,
				I_Product.ISCOM, 
				I_Product.ISSCALE,
				I_Product.PRICEBUY, 
				I_Product.PRICESELL, 
				I_Product.TAXCAT,
				I_Product.CATEGORY, 
				I_Product.ATTRIBUTESET_ID,
				// new Field("STOCKCOST", Datas.DOUBLE, Formats.CURRENCY),
				// new Field("STOCKVOLUME", Datas.DOUBLE, Formats.DOUBLE),
				// new Field("ISCATALOG", Datas.BOOLEAN, Formats.BOOLEAN),
				// new Field("CATORDER", Datas.INT, Formats.INT),
				I_Product.ATTRIBUTES,
				I_Product.CUSTOMERPRICESELL,
				I_Product.ORIGIN,
				I_Product.QUANTITY,
				I_Product.UNIT, 
				I_Product.MANUFACTUR,
				I_Product.SIMILARCODE, 
				I_Product.DURABILITY, 
				I_Product.MNEMONIC, 
				I_Product.LASTMODIFIED, 
				I_Product.MANUFACTURING,
				I_Product.WHOLESALEPRICE,
				I_Product.CURDATE,
				I_Product.WEIGHT,
				I_Product.ISBOMSTORE,
				I_Product.SPECIFICATION
		};
	}

	@Override
	public ContentValues createContentValues(Product obj) {
		ContentValues values=new ContentValues();
		values.put(I_Product.ID, obj.getID());
		values.put(I_Product.REFERENCE, obj.getReference());
		values.put(I_Product.CODE, obj.getCode());
		values.put(I_Product.NAME, obj.getName());
		values.put(I_Product.ISCOM, obj.isCom());
		
		values.put(I_Product.ISSCALE, obj.isScale());
		values.put(I_Product.PRICEBUY, obj.getPriceBuy());
		values.put(I_Product.PRICESELL, obj.getPriceSell());
		values.put(I_Product.TAXCAT, obj.getTaxCategoryID());
		values.put(I_Product.CATEGORY, obj.getCategoryID());
		
		values.put(I_Product.ATTRIBUTESET_ID, obj.getAttributeSetID());
		values.put(I_Product.ATTRIBUTES, DataLogicUtil.writeSerializable(obj.getProperties()));
		values.put(I_Product.CUSTOMERPRICESELL, obj.getM_dCustomerPrice());
		values.put(I_Product.ORIGIN, obj.getM_origin());
		values.put(I_Product.QUANTITY, obj.getM_quantity());
		
		values.put(I_Product.UNIT, obj.getM_unit());
		values.put(I_Product.MANUFACTUR, obj.getM_sManufacturer());
		values.put(I_Product.SIMILARCODE, obj.getM_sSimilarCode());
		values.put(I_Product.DURABILITY, obj.getM_iDurability());
		values.put(I_Product.MNEMONIC, obj.getMnemonic());
		
		values.put(I_Product.LASTMODIFIED, obj.getLastModified()==null?null:obj.getLastModified().getTime());
		values.put(I_Product.MANUFACTURING, obj.isM_bManufacturing());
		values.put(I_Product.WHOLESALEPRICE,obj.getM_dWholesalePrice());
		values.put(I_Product.CURDATE,obj.getCurDate()==null?null:obj.getCurDate().getTime());
		values.put(I_Product.WEIGHT, obj.getWeight());
		
		values.put(I_Product.ISBOMSTORE,obj.isBomStore());
		values.put(I_Product.SPECIFICATION,obj.getSpecification());
		return values;
	}

	@Override
	public String getTableName() {
		return Table.PRODUCTS;
	}

	@Override
	public Class getSuportClass() {	
		return Product.class;
	}

	public Product getProductInfoByReference(String codeOrReference) {
		Cursor cursor = null;
	    SQLiteDatabase db = null;
	    try {
	      db = helper.getReadableDatabase();
	      cursor = db.query(getTableName(),
	    		  getTableColumns(), 
	    		  I_Product.REFERENCE+ "=?",
                  new String[] {codeOrReference},
                  null, null, null, null);
	      while (cursor.moveToNext()) {
	        return this.readValues(cursor, null);
	      }
	      return null;
	    } finally {
	      close(cursor, db);
	    }
	}

	public List<Product> getProductInfoByCode(String codeOrReference) {
		List<Product> items = new ArrayList<Product>();
	    Cursor cursor = null;
	    SQLiteDatabase db = null;
	    try {
	      db = helper.getReadableDatabase();
	      cursor = db.query(getTableName(),
	    		  getTableColumns(), 
	    		  I_Product.CODE+ "=?",
                  new String[] {codeOrReference},
                  null, null, null, null);
	      while (cursor.moveToNext()) {
	        items.add(this.readValues(cursor, null));
	      }
	      return items;
	    } finally {
	      close(cursor, db);
	    }
	}

	public List<Product> getBomIncludeProductList(String substring) {
		return new ArrayList();
	}

	public List<Product> getBomProductForIncludeList(String substring) {
		
		return new ArrayList();
	}
	
	
	
}
